"""
分页组件
 1.在视图函数里面
         # 分页获取数据
        queryset = models.Issues.objects.filter(project_id=project_id)  # 所有数据
        page_obj = Pagination(
            current_page=request.GET.get('page'),
            all_count=queryset.count(),
            base_url=request.path_info,
            query_params=request.GET,
            per_page=1
        )

        # 拿到数据库的索引数据
        issues_obj_list = queryset[page_obj.start:page_obj.end]
        form = IssuesModelForm(request)
        context = {
            'issues_obj_list': issues_obj_list,
            'page_html': page_obj.page_html()
        }
        return render(request, 'web/issues.html', context)
        2. 前端页面
                {% for item in  issues_obj_list %}
                    展示的数据
                {% endfor %}
                <nav aria-label="Page navigation example">
                    <ul class="pagination">
                        {{ page_html|safe }}
                    </ul>
                </nav>
"""


class Pagination(object):
    def __init__(self, current_page, all_count, base_url, query_params, per_page=30, pager_page_count=11):
        """
        初始化
        :param current_page: 当前页面
        :param all_count: 数据库数据总数
        :param base_url: 基础url
        :param query_params: 包含使用url的原始参数
        :param per_page: 每页展示数据量
        :param pager_page_count: 显示最多页面数量
        """
        self.base_url = base_url
        try:
            self.current_page = int(current_page)
            if self.current_page <= 0:
                self.current_page = 1
        except Exception as e:
            self.current_page = 1
        query_params = query_params.copy()
        query_params._mutable = True
        self.query_params = query_params
        self.per_page = per_page
        self.all_count = all_count
        self.pager_page_count = pager_page_count
        pager_count, b = divmod(all_count, per_page)
        if b != 0:
            pager_count += 1
        self.pager_count = pager_count
        half_pager_page_count = int(pager_page_count / 2)
        self.half_pager_page_count = half_pager_page_count

    @property
    def start(self):
        """
        数据获取值起始索引
        :return:
        """
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        """
        数据获取结束索引
        :return:
        """
        return self.current_page * self.per_page

    def page_html(self):
        """
        生成html代码
        :return:
        """
        if self.all_count == 0:
            return ""
        # 如果数据总页码小于页面显示总页码
        if self.pager_count < self.pager_page_count:
            pager_start = 1
            pager_end = self.pager_count
        else:
            # 数据页码超过了页面显示总页码
            # 如果当前也小于5
            if self.current_page <= self.half_pager_page_count:
                pager_start = 1
                pager_end = self.pager_page_count
            else:
                # 如果当前页码 + 5 大于总页码
                if self.current_page + self.half_pager_page_count > self.pager_count:
                    pager_end = self.pager_count
                    pager_start = self.pager_count - self.pager_page_count + 1
                else:
                    pager_start = self.current_page - self.half_pager_page_count
                    pager_end = self.current_page + self.half_pager_page_count
        page_list = []
        if self.current_page <= 1:
            prev = '<li><a href="#">上一页</a></li>'
        else:
            self.query_params['page'] = self.current_page - 1
            prev = '<li><a href="%s?%s">上一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(prev)
        for i in range(pager_start, pager_end + 1):
            self.query_params['page'] = i
            if self.current_page == i:
                tpl = '<li class="active"><a href="%s?%s">%s</a></li>' % (
                self.base_url, self.query_params.urlencode(), i)
            else:
                tpl = '<li><a href="%s?%s">%s</a></li>' % (
                    self.base_url, self.query_params.urlencode(), i)
            page_list.append(tpl)
        if self.current_page >= self.pager_count:
            nex = '<li><a href="#">下一页</a></li>'
        else:
            self.query_params['page'] = self.current_page + 1
            nex = '<li><a href="%s?%s">下一页</a></li>' % (self.base_url, self.query_params.urlencode())
        page_list.append(nex)
        if self.all_count:
            tpl = '<li class="disabled"><a>共%s条数据，页码%s/%s</a></li>' % (
                self.all_count,
                self.current_page,
                self.pager_count
            )
            page_list.append(tpl)
        page_str = ''.join(page_list)
        return page_str


    """
        nav .pagination li {
    display: inline-block;           /* 水平排列 */
    margin: 0 2px;                  /* 项间距 */
    list-style: none;               /* 去除列表样式 */
}

nav .pagination li a {
    display: block;                 /* 让整个区域可点击 */
    padding: 8px 12px;             /* 内边距 */
    text-decoration: none;          /* 去除下划线 */
    color: #007bff;                /* 链接颜色 */
    border: 1px solid #dee2e6;     /* 边框 */
    border-radius: 4px;            /* 圆角 */
    transition: all 0.2s ease;     /* 过渡动画 */
    font-size: 14px;
}

/* 悬停状态 */
nav .pagination li a:hover {
    background-color: #e9ecef;
    border-color: #adb5bd;
}

/* 当前页激活状态 */
nav .pagination li.active a {
    background-color: #007bff;
    border-color: #007bff;
    color: white;
}

/* 禁用状态 */
nav .pagination li.disabled a {
    color: #6c757d;
    pointer-events: none;
    background-color: #fff;
    border-color: #dee2e6;
    cursor: not-allowed;
}

/* 特殊按钮样式（上一页/下一页） */
nav .pagination li.prev a,
nav .pagination li.next a {
    padding: 8px 16px;
    font-weight: 500;
}
    """
